package com.bringway.p2p.dao.impl;

import java.lang.annotation.Retention;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.object.SqlQuery;
import org.springframework.stereotype.Repository;

import com.bringway.framework.dao.P2PJdbcDao;
import com.bringway.framework.util.RequestUtils;
import com.bringway.global.util.StringUtils;
import com.bringway.p2p.dao.P2PCustomerDao;

import net.sf.json.JSONObject;

/*
 * @author fan
 * @className P2PCustomerDaoImpl
 * @description 客户信息持久层dao实现类
 * @date 2016年1月28日下午2:12:26
 */
@Repository
public class P2PCustomerDaoImpl extends P2PJdbcDao implements P2PCustomerDao {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.bringway.p2p.dao.P2PCustomerDao#getCustomerFeedBackInfo(net.sf.json.
	 * JSONObject)
	 */
	@Override
	public List getCustomerFeedBackInfo(JSONObject parms) {
		String ADVICE_START_DATE = parms.optString("ADVICE_START_DATE");
		String ADVICE_END_DATE = parms.optString("ADVICE_END_DATE");
		String USER_ID = parms.optString("USER_ID");
		String LXFS = parms.optString("LXFS");
		String DEVICE_TYPE = parms.optString("DEVICE_TYPE");
		int start = parms.optInt("start");
		int limit = parms.optInt("limit");
		StringBuffer sql = new StringBuffer();
		Map<String, Object> parmsMap = new HashMap<String, Object>();
		sql.append(" select v.ID,v.ADVICE,DATE_FORMAT(v.ADVICE_DATE,'%Y-%m-%d %T') as ADVICE_DATE,v.DEVICE_TYPE,v.LXFS,v.USER_ID from v_p2p_user_advice v ");
		sql.append(" where 1=1 ");
		if (StringUtils.isNotEmpty(ADVICE_START_DATE)) {
			sql.append(" and v.ADVICE_DATE >= :ADVICE_START_DATE ");
			parmsMap.put("ADVICE_START_DATE", ADVICE_START_DATE+" 00:00:00");
		}
		if (StringUtils.isNotEmpty(ADVICE_END_DATE)) {
			sql.append(" and v.ADVICE_DATE <= :ADVICE_END_DATE ");
			parmsMap.put("ADVICE_END_DATE", ADVICE_END_DATE+" 23:59:59");
		}
		if (StringUtils.isNotEmpty(USER_ID)) {
			sql.append(" and v.USER_ID like :USER_ID ");
			parmsMap.put("USER_ID", "%" + USER_ID + "%");
		}
		if (StringUtils.isNotEmpty(LXFS)) {
			sql.append(" and v.LXFS like :LXFS ");
			parmsMap.put("LXFS", "%" + LXFS + "%");
		}
		if (StringUtils.isNotEmpty(DEVICE_TYPE)) {
			sql.append(" and v.DEVICE_TYPE = :DEVICE_TYPE ");
			parmsMap.put("DEVICE_TYPE", DEVICE_TYPE);
		}
		sql.append(" order by v.ADVICE_DATE desc limit :start,:limit ");
		parmsMap.put("start", start);
		parmsMap.put("limit", limit);
		return this.getNamedParameterJdbcTemplate().queryForList(sql.toString(), parmsMap);
	}
	/*
	 * (non-Javadoc)
	 * @see com.bringway.p2p.dao.P2PCustomerDao#getPageCount(net.sf.json.JSONObject)
	 */
	@Override
	public int getPageCount(JSONObject parms) {
		String ADVICE_START_DATE = parms.optString("ADVICE_START_DATE");
		String ADVICE_END_DATE = parms.optString("ADVICE_END_DATE");
		String USER_ID = parms.optString("USER_ID");
		String LXFS = parms.optString("LXFS");
		String DEVICE_TYPE = parms.optString("DEVICE_TYPE");
		int start = parms.optInt("start");
		int limit = parms.optInt("limit");
		StringBuffer sql = new StringBuffer();
		Map<String, Object> parmsMap = new HashMap<String, Object>();
		sql.append(" select count(*) from v_p2p_user_advice v ");
		sql.append(" where 1=1 ");
		if (StringUtils.isNotEmpty(ADVICE_START_DATE)) {
			sql.append(" and v.ADVICE_DATE >= :ADVICE_START_DATE ");
			parmsMap.put("ADVICE_START_DATE", ADVICE_START_DATE+" 00:00:00");
		}
		if (StringUtils.isNotEmpty(ADVICE_END_DATE)) {
			sql.append(" and v.ADVICE_DATE <= :ADVICE_END_DATE ");
			parmsMap.put("ADVICE_END_DATE", ADVICE_END_DATE+" 23:59:59");
		}
		if (StringUtils.isNotEmpty(USER_ID)) {
			sql.append(" and v.USER_ID like :USER_ID ");
			parmsMap.put("USER_ID", "%" + USER_ID + "%");
		}
		if (StringUtils.isNotEmpty(LXFS)) {
			sql.append(" and v.LXFS like :LXFS ");
			parmsMap.put("LXFS", "'%" + LXFS + "%'");
		}
		if (StringUtils.isNotEmpty(DEVICE_TYPE)) {
			sql.append(" and v.DEVICE_TYPE = :DEVICE_TYPE ");
			parmsMap.put("DEVICE_TYPE", DEVICE_TYPE);
		}
		return this.getNamedParameterJdbcTemplate().queryForObject(sql.toString(), parmsMap,Integer.class);
	}
}
